//Attach this to a GUIText to make a frames/second indicator
using UnityEngine;
using System.Collections;

[RequireComponent(typeof(GUIText))]

public class FPS : MonoBehaviour
{
	public float updateInterval = 0.5f;
	
	private float accum = 0.0f;
	private int frames = 0;
	private float timeLeft;
	
	public void Start()
	{
		if(!guiText)
		{
			print("FramesPerSecond needs a GUIText component!");
			enabled = false;
			return;
		}	
		timeLeft = updateInterval;
	}
	
	public void Update()
	{
		timeLeft -= Time.deltaTime;
		accum += Time.timeScale / Time.deltaTime;
		++frames;
		
		//Interval ended - update GUI text and start new interval
		if(timeLeft <= 0.0f)
		{
			//display two fractional digits (f2 format)
			guiText.text = "" + (accum/frames).ToString("f2");
			timeLeft = updateInterval;
			accum = 0.0f;
			frames = 0;
		}
	}
	
}